Skip to content

Added information on how to use Vec<T> with Client::query_raw #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2020

Conversation

runfalk
Copy link
Contributor

@runfalk runfalk commented Feb 5, 2020

The examples in the rendered documentation looks like this:

use tokio_postgres::types::ToSql;
use futures::StreamExt;

let params: Vec<String> = vec![
    "first param".into(),
    "second param".into(),
];
let mut it = Box::pin(client.query_raw(
    "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
    params.iter().map(|p| p as &dyn ToSql),
).await?);

while let Some(row) = it.next().await.transpose()? {
    let foo: i32 = row.get("foo");
    println!("foo: {}", foo);
}
use postgres::types::ToSql;
use fallible_iterator::FallibleIterator;

let params: Vec<String> = vec![
    "first param".into(),
    "second param".into(),
];
let mut it = client.query_raw(
    "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
    params.iter().map(|p| p as &dyn ToSql),
)?;

while let Some(row) = it.next()? {
    let foo: i32 = row.get("foo");
    println!("foo: {}", foo);
}

I tested the code using cargo test --doc and rendered it locally using cargo doc. I did not know how to run the full suite.

Do you think the Box::pin bit is fine or is there a leaner way to do it?

@sfackler sfackler merged commit 14252af into sfackler:master Feb 9, 2020
@sfackler
Copy link
Owner

sfackler commented Feb 9, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants